﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NJL.WebManage.LocalColor
{
    public partial class AddLocalColor : BasePage
    {
        private readonly NJL.BLL.NJL_LOCAL_COLOR bll = new BLL.NJL_LOCAL_COLOR();
        private readonly NJL.BLL.NJL_CS_LOCAL_COLOR_TYPE bllType = new BLL.NJL_CS_LOCAL_COLOR_TYPE();
        private readonly NJL.BLL.NJL_CS_AREA bllArea = new BLL.NJL_CS_AREA();
 
        NJL.Model.NJL_LOCAL_COLOR model = new Model.NJL_LOCAL_COLOR();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //地区
                //DataSet ds = bllArea.GetList("AREA_CODE like '%0000'");
                ddlProvince.DataSource = bllArea.GetList("AREA_CODE like '%0000' and deep=1");
                ddlProvince.DataTextField = "AREA_NAME";
                ddlProvince.DataValueField = "AREA_CODE";
                ddlProvince.DataBind();
                if (WEBSITE_CODE == "1")
                {
                }
                else
                {
                    ddlProvince.Visible = false;
                    string ProvinceId = WEBSITE_CODE.Substring(0, 2);
                    DataSet dscity = bllArea.GetList("AREA_CODE like '" + ProvinceId + "%00' and deep=2");
                    ddlCity.DataSource = dscity;
                    ddlCity.DataTextField = "AREA_NAME";
                    ddlCity.DataValueField = "AREA_CODE";
                    ddlCity.DataBind();
                }

                DataSet ds = bllType.GetList("");      
                cbxList.DataTextField = "TYPE_NAME";
                cbxList.DataValueField = "TYPE_CODE";
                cbxList.DataSource = ds;
                cbxList.DataBind();               
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {  
            model.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
            if (FileUpload1.HasFile)
            {
                model.IMAGE_CODE = SaveUploadFile(Request.Files);
            }
            model.NJL_SYS_BASE_ID = WEBSITE_CODE;
            model.SEO_DESCRIPTION = txtDescription.Text.Trim();
            model.SEO_KEYWORDS = txtKeyWords.Text.Trim();
            model.SEO_TITLE = txtTitle.Text.Trim();
            model.NEWS_TITLE = txtAttractionsTitle.Text;
            model.NEWS_SUBJECT_TITLE = txtAttractionFtitle.Text;
            model.NEWS_TAG = Royalsoft.Common.StringPlus.ToDBC(txtTag.Text);
            model.NEWS_CONTECT = txtContent.Text;
            model.CREATE_USER_NAME = USER_NAME;
            model.CREATE_USER_ID = USER_ID;
            model.CREATE_DATE_TIME = DateTime.Now;
            model.PUBLISH_DATE_TIME = DateTime.Now;
            model.SORT = int.Parse(txtSort.Text.Trim());
            model.IS_TOP = 0;
            model.IS_INDEX = 0;
            model.IS_PUBLISH = 0;
            model.TYPE_CODE = cbxList.SelectedValue;
            model.AREA_CODE = ddlCity.SelectedValue;
            model.IS_MERCHANTS = Convert.ToInt32(rblIsZs.SelectedValue);
            model.MERCHANTS_AD = txtAd.Text.Trim();
            if (bll.Add(model))
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "添加成功", "AddLocalColor.aspx");
                #region 标签处理
                NJL.BLL.NJL_CS_TAG bllTag = new BLL.NJL_CS_TAG();
                NJL.Model.NJL_CS_TAG modelTag = new Model.NJL_CS_TAG();
                char[] tagtxt1 = { ',', '，' };
                string[] tag = txtTag.Text.Trim().Split(tagtxt1);
                foreach (string strtag in tag)
                {
                    DataSet ds = bllTag.GetList("TAG='" + strtag + "' and NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        modelTag = bllTag.GetModel(ds.Tables[0].Rows[0][0].ToString());
                        if (modelTag != null)
                        {
                            if (modelTag.USED_COUNT != null)
                            {
                                modelTag.USED_COUNT += 1;
                                if (bllTag.Update(modelTag))
                                {
                                }
                            }
                        }
                    }
                    else
                    {
                        if (strtag != "")
                        {
                            modelTag.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random(Guid.NewGuid().GetHashCode()).Next(100, 999).ToString();
                            modelTag.IS_SYS_TAG = 0;
                            modelTag.TAG = strtag;
                            modelTag.CREATE_DATE_TIME = DateTime.Now;
                            modelTag.NJL_SYS_BASE_ID = WEBSITE_CODE;
                            modelTag.SEARCH_COUNT = 0;
                            modelTag.TAG_TYPE = "2";  //地方特色是2
                            modelTag.USED_COUNT = 0;
                            modelTag.AREA_CODE = ddlCity.SelectedValue;
                            bllTag.Add(modelTag);
                        }
                    }
                }
                #endregion 
            }
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ProvinceId = ddlProvince.SelectedValue.Substring(0, 2);
            DataSet ds = bllArea.GetList("AREA_CODE like '" + ProvinceId + "%00' and deep=2");
            ddlCity.DataSource = ds;
            ddlCity.DataTextField = "AREA_NAME";
            ddlCity.DataValueField = "AREA_CODE";
            ddlCity.DataBind();
        }
    }
}